<?php

namespace Admin\Controller;

class AccountController extends AdminController {

    public function index() {
        $nickname = I('nickname', '');
        $mobile = I('mobile');
        $date_range = I('date_range', '');
        $keyword = I('keyword');
        $where = [];

        $prefix = C('DB_PREFIX');
        
        if ($mobile) {
            $where["B.mobile"] = $mobile;
        }
        if ($nickname) {
            $user = M('User')->where(array('nickname'=>$nickname))->find();  
            $where["B.id"] = $user['id'];
        }
        
        if ($date_range) {
            list($start_date, $end_date) = explode('~', $date_range);
            $where["A.created_at"] = ['between', [strtotime($start_date), strtotime($end_date) + (3600 * 24) - 1]];
        }
        if($keyword){
            $where["A.remark"]=['like','%'.$keyword.'%'];
        }
            
        $total = M('UserAccount')->alias('A')->join($prefix.'user as B ON A.user_id = B.id','left')->where($where)->count();

        $Page = new \Think\Page($total, 20);
        $result = M('UserAccount')->alias('A')->field("A.*,B.nickname,B.mobile,B.identity")
            ->join($prefix.'user as B ON A.user_id = B.id','left')
            ->page($Page->nowPage, $Page->listRows)
            ->order("id DESC")
            ->where($where)->select();
        
        $this->assign('result', $result);
        $this->assign('page', $Page->show());
        $this->display();
    }
    
    public function sys(){
        $nickname = I('nickname', '');
        $mobile = I('mobile');
        $date_range = I('date_range', '','urldecode');
        $where = array();

        if ($mobile) {
            $where["B.mobile"] = $mobile;
        }
        if ($nickname) {
            $user = M('User')->where(array('nickname'=>$nickname))->find();  
            $where["A.user_id"] = $user['id'];
        }
        if ($date_range) {
            list($start_date, $end_date) = explode('~', $date_range);
            $where["A.created_at"] = ['between', [strtotime($start_date), strtotime($end_date) + (3600 * 24) - 1]];
        }
        
        $count = M('SysAccount')->alias('A')->field('A.id')
                ->join(C('DB_PREFIX').'user B ON A.user_id = B.id','left')->where($where)->count();
        $Page = new \Think\Page($count);
        
        $records = M('SysAccount')->alias('A')->field('A.*,B.nickname,B.mobile,B.identity')
                ->join(C('DB_PREFIX').'user B ON A.user_id = B.id','left')
                ->where($where)->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
        //计算总数
        $total = M('SysAccount')->alias('A')->field('A.*')
            ->join(C('DB_PREFIX').'user B ON A.user_id = B.id','left')
            ->where($where)->sum('A.amount');
        $this->assign(array(
            'result'   =>  $records,
            'page'      =>  $Page->show(),
            'total'     =>  sprintf('%.2f',$total),
        ));
        $this->display();
    }

}
